from os import popen4
from os import path


class DirectForceFieldJob:
    @staticmethod
    def make_input(script, output):
        START_TAG = """#Direct Force Field Job
    from datetime import datetime
    print 'Job Started at', datetime.now()
    try
    """
        FINISH_TAG = """
    except RuntimeError, error:
        print '#Error:', error
    print 'Job Finished at', datetime.now()
    
    """
        pin.write(START_TAG);
        pin.write(script);
        pin.write(FINISH_TAG);
        pin.flush();
    
    @staticmethod
    def find_exe(exename):
        var = globals()
        exename += ".exe"
        if "DFF_BIN" in var:
            folder = var["DFF_BIN"]
            fullname = path.join(folder, exename)
            if path.isfile(fullname):
                return fullname
        return exename
    
    def __init__(self, exename="dffjob"):
        exename = DirectForceFieldJob.find_exe(exename)
        self.pin, self.pout, self.perr = popen3(exename)